﻿/*
 * 合同一览页面
 * 
 * 2009-4-1
 * */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using EQT.Dal;
using EQT.Model;

public partial class Contract : System.Web.UI.Page
{
    private DaContract dacontract = new DaContract(DaCommon.DefaultDb);
    private DaSysCoding dasc = new DaSysCoding(DaCommon.DefaultDb);
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {           
            //权限检查
            bool can_edit_contracts=Utility.CheckRights(FuncIdConstants.DTT_MANAGE_CONTRACTS, this);
            GridViewContracts.Columns[8].Visible = can_edit_contracts;
            GridViewContracts.Columns[7].Visible = can_edit_contracts;

            //绑定合同状态下拉列表
            Utility.BindListCtl2Dic(DropDownListStatus, ContractStatusCollection.CommonGrpStatus);
            InitDefaultFitler();
            LoadContractList();


          
        }
    }

    private void InitDefaultFitler()
    {
        DateTime default_begin_day = new DateTime(DateTime.Today.Year, 1, 1);
        TextBoxBegin.Text = default_begin_day.ToString(SiteConfig.DateFormat);
        TextBoxEnd.Text = DateTime.Now.AddDays(1).ToString(SiteConfig.DateFormat);
        DropDownListStatus.SelectedIndex = DropDownListStatus.Items.IndexOf(DropDownListStatus.Items.FindByValue(ContractStatus.ALL.ToString()));
        TextBoxReportId.Text = String.Empty;
        TextBoxCorpName.Text = String.Empty;

        AspNetPager1.PageSize = SiteConfig.PageSize;

    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        LoadContractList();
    }

  
   
    
  
    /// <summary>
    /// 单击了送检按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnChangeTo2000_Click(object sender, EventArgs e)
    {
        Button btn = sender as Button;
        if (btn != null)
        {
            string report_id= btn.CommandArgument;
            //处理这个报表
            if (!string.IsNullOrEmpty(report_id))
            {              
                //签订合同并送检
                dacontract.ReplaceContractStatus(report_id, (int)ContractStatus.WaitTest);
                LoadContractList();
                return;
            }
        }
        Utility.SiteError(String.Format("BtnChangeTo2000_Click ==>{0}命令失败！", sender));
    }
    /// <summary>
    /// 合同绑定行时，决定是否显示【送检】按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridViewContracts_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        
        if (e.Row.RowType == DataControlRowType.DataRow && GridViewContracts.Columns[8].Visible)
        {
          
            int report_status = Convert.ToInt32(((DataRowView)e.Row.DataItem)["REPORT_STATUS"]);
            report_status &= DaContract.CLEAR_PAY_FLAG;//清除不影响测试组业务的标志
            ((Button)e.Row.FindControl("BtnChangeTo2000")).Visible = (report_status == (int)ContractStatus.Signed);           

        }
    }

    /// <summary>
    /// 加载合同列表
    /// </summary>
    private void LoadContractList()
    {
        #region 构建过滤字符串
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        //时间范围是必须的
        sb.Append(DaContract.SELECT_CONTRACTS_FILEDS);
        sb.Append(Utility.GenerateQueryFilter2DateTime("CREATE_TIME", TextBoxBegin.Text, TextBoxEnd.Text));
        string filter = Utility.GenerateQueryFilterString("Report_ID", TextBoxReportId.Text, 2);
        if (filter != null) sb.AppendFormat(" AND {0} ", filter);
        filter = Utility.GenerateQueryFilterString("Corp_Name", TextBoxCorpName.Text, 2);
        if (filter != null) sb.AppendFormat(" AND {0} ", filter);

        sb.AppendFormat(" AND {0}", Utility.GenerateQueryFilterIntMaskAnd("Report_Status", "&", Int32.Parse(DropDownListStatus.SelectedValue)));
        string sql = sb.ToString();
        if (Utility.GlobalLog.IsDebugEnabled) Utility.GlobalLog.Debug(sql);
        int totalCount = 0;
        #endregion

        DataSet ds = DaCommon.GetPaginationData(sql,
            SiteConfig.PageSize, AspNetPager1.CurrentPageIndex - 1, "Create_Time DESC", out totalCount);

        AspNetPager1.RecordCount = totalCount;
        GridViewContracts.DataSource = ds;
        GridViewContracts.DataBind();

    }

    /// <summary>
    /// 条件查询
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnFilterQuery_Click(object sender, EventArgs e)
    {
        AspNetPager1.CurrentPageIndex = 0;
        LoadContractList();
    }
    /// <summary>
    /// 查询全部
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnQueryAll_Click(object sender, EventArgs e)
    {
        AspNetPager1.CurrentPageIndex = 0;
        InitDefaultFitler();
        LoadContractList();
    }
}
